<?php
namespace App\Pay\Gateway\Alipay;

use App\Pay\Config\Config;
use App\Pay\Gateway\Alipay;
use GuzzleHttp\Client;

class Request
{
    /**
     * @var Config
     */
    private $config;

    /**
     * @var \App\Pay\Gateway\Alipay\Request
     */
    private static $instance;

    /**
     * @var string
     */
    protected $url;

    /**
     * @var Client
     */
    protected $client;

    /**
     * @var array
     */
    protected $result;

    public static function instance()
    {
        if (is_null(self::$instance)) {
            return self::$instance = new static();
        }
        return self::$instance;
    }

    /**
     * @param Config $config
     */
    public function setConfig(Config $config)
    {
        $this->config = $config;
        $this->url = Alipay::URL[$config->get('mode', Alipay::MODE_NORMAL)];
        $this->client = new Client(['base_uri' => $this->url]);
    }

    /**
     * @return Config
     */
    public function config()
    {
        return $this->config;
    }

    public static function api($params)
    {
        return self::$instance->post($params);
    }

    protected function post($params)
    {
        $response = $this->client->post('', ['form_params' => $params]);
        dd($response->getBody()->getContents());
    }

    public static function url()
    {
        return self::$instance->url;
    }
}
